﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using DomainModel.Entities;
using NHibernate;
using NHibernate.Criterion;

namespace DomainModel.Concrete
{
    public class EventsRSVPRepository : IEventsRSVPRepository
    {
        public IEnumerable<EventRSVP> GetAll()
        {
            ICriteria criteria = SessionManager.CurrentSession.CreateCriteria(typeof(EventRSVP)).AddOrder(Order.Desc("RSVPID"));
            return criteria.List<EventRSVP>();
        }

        public EventRSVP Get(int? id)
        {
            return SessionManager.CurrentSession.Get<EventRSVP>(id);
        }

        public EventRSVP GetByEventIDAndUserID(int eventID, int userID)
        {
            ICriteria criteria = SessionManager.CurrentSession.CreateCriteria(typeof(EventRSVP));
            return criteria.List<EventRSVP>().Where(x=>x.EventID==eventID).Where(x=>x.UserID==userID).FirstOrDefault();
        }
         
        public void Save(EventRSVP item)
        {
            ISession session = SessionManager.CurrentSession;
            session.BeginTransaction();
            session.Save(item);
            session.Transaction.Commit();
        }

        public void Merge(EventRSVP item)
        {
            ISession session = SessionManager.CurrentSession;
            session.BeginTransaction();
            session.Merge(item);
            session.Transaction.Commit();
        }

        public void Delete(int id)
        {
            SessionManager.CurrentSession.Delete(Get(id));
        }

        public void DeleteByEventIDAndUserID(int eventID, int userID)
        {
            var item = GetByEventIDAndUserID(eventID, userID);
            SessionManager.CurrentSession.Delete(item);
           
        }
    }
}
